﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Search
{
    public class Array
    {
        private int[] Results;
        private int[] tmp;

        private bool Search_Contains(string parameter, string text)
        {
            for (int i = 0; i <= text.Length - parameter.Length; i++)
            {
                if (text.Substring(i, parameter.Length).ToLower() == parameter.ToLower())
                    return true;
            }
            return false;
        }
        private void Search_Filter(int[] array)
        {
            bool usetmp = false;
            int tmp = 0;
            int tmpID = 0;
            int offset = 0;
            int EO= 0;
            int[] tmplist;

            // Filter Duplicate Entries
            for (int i = 0; i < Results.Length; i++)
            {
                for (int c = i + 1; c < Results.Length; c++)
                {
                    if (Results[i] == Results[c])
                    {
                        tmplist = new int[Results.Length];
                        for (int f = 0; f < Results.Length; f++)
                            tmplist[f] = Results[f];
                        Results = new int[Results.Length - 1];
                        for (int f = 0; f < tmplist.Length; f++)
                        {
                            if (c != f)
                            {
                                Results[f - offset] = tmplist[f];
                            }
                            else
                                offset += 1;
                        }
                        offset = 0;
                    }
                }
            }

            // Double Check (Yes you may think this is redundant, but there
            // are random scenarios where a random double pops up, and this
            // was the easiest way...so no u.
            for (int i = 0; i < Results.Length; i++)
            {
                for (int c = i + 1; c < Results.Length; c++)
                {
                    if (Results[i] == Results[c])
                    {
                        tmplist = new int[Results.Length];
                        for (int f = 0; f < Results.Length; f++)
                            tmplist[f] = Results[f];
                        Results = new int[Results.Length - 1];
                        for (int f = 0; f < tmplist.Length; f++)
                        {
                            if (c != f)
                            {
                                Results[f - offset] = tmplist[f];
                            }
                            else
                                offset += 1;
                        }
                        offset = 0;
                    }
                }
            }

            // Organize
            offset = 0;
            tmplist = new int[Results.Length];
            for (int f = 0; f < Results.Length; f++)
                tmplist[f] = Results[f];

            for (int i = 0; i < Results.Length; i++)
            {
                for (int f = 0; f < Results.Length; f++)
                    tmplist[f] = Results[f];

                tmp = Results[i];
                for (int c = i; c < Results.Length; c++)
                {
                    if (Results[c] < tmp)
                    {
                        tmp = Results[c];
                        tmpID = c;
                        usetmp = true;
                    }
                    EO = c;
                }
                Results[i] = tmp;
                if (usetmp == true)
                    Results[tmpID] = tmplist[i];
                else
                    Results[EO] = tmplist[EO];
                usetmp = false;
            }
        }
        public int[] Search(string search, string[] array)
        {
            for (int c = 0; c <array.Length; c++)
            {
                if (Search_Contains(search, array[c]) == true)
                {
                    if (Results.Length > 0)
                    {
                        tmp = new int[Results.Length];
                        for (int i = 0; i < Results.Length; i++)
                            tmp[i] = Results[i];

                        Results = new int[Results.Length + 1];
                        for (int i = 0; i < Results.Length; i++)
                            Results[i] = tmp[i];

                        Results[Results.Length - 1] = c;
                    }
                    else
                    {
                        Results = new int[1];
                        Results[0] = c;
                    }
                }
            }
            Search_Filter(Results);
            return Results;
        }
    }
}
